/*
 * Copyright 2008-2012 Centro Andaluz de Innovación y Tecnologías de la
 * Información y las Comunicaciones (CITIC). All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 *
 * Please contact CITIC - Edificio CITIC, C/ Marie Curie, 6 Parque
 * Tecnológico de Andalucía 29590 - Campanillas, Málaga, Spain
 * or visit www.citic.es, if you need additional information or
 * have any questions.
 */

package es.citic.puedes.youtube.testhelp;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import es.citic.puedes.youtube.IYouTubeManager;
import es.citic.puedes.youtube.YouTubeAuthException;
import es.citic.puedes.youtube.YouTubeException;
import es.citic.puedes.youtube.model.Category;
import es.citic.puedes.youtube.model.YouTubeUser;
import es.citic.puedes.youtube.model.YouTubeVideo;
import es.citic.puedes.youtube.model.YouTubeVideo.State;
import es.citic.puedes.youtube.model.YouTubeVideoMinimal;
import es.citic.puedes.youtube.test.YouTubeManagerTest;

public class YouTubeVideoTest {	
	public static void runYouTubeRateVideoTest(IYouTubeManager ytm, Integer userId, String videoID) throws SecurityException, YouTubeException, YouTubeAuthException {
		try {
			ytm.registreUser(userId, YouTubeManagerTest.username, YouTubeManagerTest.pass);
		} catch (Exception e) {
			// Nothing to do
		}

		// getting user profile
		YouTubeUser userProfile = ytm.getUserProfile(userId);

		// values from 1 to 3 adds the video to 'disliked videos' list and
		// values from 4 to 5 adds video to 'liked video' list
		Integer ratingValue = 5;
		ytm.addRating(userProfile, videoID, ratingValue);
	}

	public static void runYouTubeCommentVideoTest(IYouTubeManager ytm, Integer userId, String videoID) throws SecurityException, YouTubeException, YouTubeAuthException {
		try {
			ytm.registreUser(userId, YouTubeManagerTest.username, YouTubeManagerTest.pass);
		} catch (Exception e) {
			// Nothing to do
		}

		// gets the user profile..		
		YouTubeUser userProfile = ytm.getUserProfile(userId);

		// adds a comment to a video..
		String comment = "comentario de prueba";
		ytm.addComment(userProfile, videoID, comment);
	}

	public static void runYouTubeUploadVideoTest(IYouTubeManager ytm, Integer userId) throws SecurityException, YouTubeException, YouTubeAuthException {
		try {
			ytm.registreUser(userId, YouTubeManagerTest.username, YouTubeManagerTest.pass);
		} catch (Exception e) {
			// Nothing to do
		}

		// getting user profile
		YouTubeUser userProfile = ytm.getUserProfile(userId);

		// test for uploading a video
		List<String> keyWordsTestList = new ArrayList<String>();
		keyWordsTestList.add("tortuga");
		keyWordsTestList.add("mascota");
		keyWordsTestList.add("animal");

		List<Category> categories = new ArrayList<Category>();
		categories.add(Category.ANIMALS);

		YouTubeVideo videoToUpload = new YouTubeVideo();
		videoToUpload.setKeyWords(keyWordsTestList);
		videoToUpload.setCategories(categories);
		videoToUpload.setPublicationState(State.PUBLIC);
		videoToUpload.setTitle("test video title");
		videoToUpload.setDescription("test video description ");

		String videoPath = "/home/david/Escritorio/Pruebas/mediaplayer-5.6/video.mp4";

		ytm.insertVideo(userProfile, videoToUpload, videoPath);
	}

	public static void runYouTubeDeleteVideoTest(IYouTubeManager ytm, Integer userId, String videoID) throws SecurityException, YouTubeException, YouTubeAuthException {
		try {
			ytm.registreUser(userId, YouTubeManagerTest.username, YouTubeManagerTest.pass);
		} catch (Exception e) {
			// Nothing to do
		}

		// getting user profile
		YouTubeUser userProfile = ytm.getUserProfile(userId);

		// test for deleting a video..
		ytm.deleteVideo(userProfile, videoID);
	}

	public static void runYouTubeGetVideoTest(IYouTubeManager ytm, String videoID) throws SecurityException, YouTubeException {	
		// test for getting a video
		YouTubeVideo video;
		video = ytm.getVideo(videoID);
		YouTubePrinterTestMethods.printModelVideo(video);
	}

	public static void runYouTubeUpdateVideoTest(IYouTubeManager ytm, Integer userId) throws SecurityException, YouTubeException, YouTubeAuthException {
		try {
			ytm.registreUser(userId, YouTubeManagerTest.username, YouTubeManagerTest.pass);
		} catch (Exception e) {
			// Nothing to do
		}

		// getting user profile
		YouTubeUser userProfile = ytm.getUserProfile(userId);

		// uploading a video
		try {
			List<String> keyWordsTestList = new ArrayList<String>();
			keyWordsTestList.add("galapagos");
			keyWordsTestList.add("fauna");
			keyWordsTestList.add("anfibio");

			List<Category> categories = new ArrayList<Category>();
			categories.add(Category.ANIMALS);

			YouTubeVideo videoToUpload = new YouTubeVideo();
			videoToUpload.setKeyWords(keyWordsTestList);
			videoToUpload.setCategories(categories);
			videoToUpload.setPublicationState(State.PUBLIC);
			videoToUpload.setTitle("test video title");
			videoToUpload.setDescription("test video description ");

			String videoPath = "/home/david/Escritorio/Pruebas/mediaplayer-5.6/video.mp4";

			ytm.insertVideo(userProfile, videoToUpload, videoPath);
		} catch (Exception e) {
			// Nothing to do
		}
		
		// get new videoID
		String videoID = null;
		try {
			List<YouTubeVideoMinimal> userVideoList = ytm.getUserVideos(userProfile);
			Iterator<YouTubeVideoMinimal> iter = userVideoList.iterator();
			YouTubeVideoMinimal userVideo = null;
			while (iter.hasNext()){
				userVideo = iter.next();
				if (userVideo.getTitle().equals("test video title")){
					return;
				}
			}
			videoID = userVideo.getVideoId();
		} catch (Exception e) {
			// Nothing to do
		}
		
		// updates an user video
		try {
			String title = "tortuga subiendo piedras";
			String description = "una gran tortuga subiendo piedras";
			List<String> keyWords = new ArrayList<String>();
			keyWords.add("tortugas");
			keyWords.add("animales");		
			ytm.updateVideo(userProfile, videoID, title, description, keyWords);
		} catch (Exception e) {
			
		}
	}

	public static void runYouTubeGetVideoPaginationTest(IYouTubeManager ytm, Integer userId) throws SecurityException, YouTubeException, YouTubeAuthException {	
		try {
			ytm.registreUser(userId, YouTubeManagerTest.username, YouTubeManagerTest.pass);
		} catch (Exception e) {
			// Nothing to do
		}

		// getting user profile
		YouTubeUser userProfile = ytm.getUserProfile(userId);

		//gets uploaded videos by paging
		List<YouTubeVideoMinimal> uploadVideoList = ytm.getUserVideos(userProfile,2,2);
		YouTubePrinterTestMethods.printModelVideoMinimalList(uploadVideoList, "UPLOAD VIDEOS BY PAGING:");
	}
}
